/**
 * SysArticleController.java -- 2017-10-19 19:47:48 作者：欧增奇 -- 代码生成器自动生成 联系方式：425752797@qq.com http://www.ouzengqi.com/
 */
package com.ozq.sys.controller.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ozq.common.dao.entity.SpringJson;
import com.ozq.common.page.Page;
import com.ozq.common.validator.Jump;
import com.ozq.common.validator.Update;
import com.ozq.sys.controller.form.SysArticleForm;
import com.ozq.sys.dao.entity.SysArticle;
import com.ozq.sys.service.inter.SysArticleService;

/**
 * <文章表控制> <功能详细描述>
 * 
 * @author 欧增奇
 * @version [V2.0, 2017年10月19日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping("sys")
public class SysArticleController
{
    static Logger logger = LoggerFactory.getLogger(SysArticleController.class);

    /**
     * 应用-文章表服务
     */
    @Autowired
    private SysArticleService sysArticleService;

    /**
     * 应用-文章表-跳转
     * 
     * @param sysArticleForm
     * @return
     */
    @RequestMapping("jump/sysArticle/to")
    public String jumpTo(Model model, @Validated({ Jump.class }) SysArticleForm sysArticleForm, BindingResult result)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysArticleForm:{}", sysArticleForm);
        }

        // 参数验证-不合法直接返回错误的页面或则错误的json
        if (result.hasErrors())
        {
            model.addAttribute("errors", result.getFieldErrors());
            return "common/error";
        }

        return "sysArticle/" + sysArticleForm.getJump();
    }

    /**
     * 应用-文章表分页查询列表-执行
     * 
     * @param model
     * @param sysArticleForm
     * @return
     */
    @RequestMapping("listPage/sysArticle/do")
    public String listPageSysArticleDo(Model model, SysArticleForm sysArticleForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysArticleForm:{}", sysArticleForm);
        }

        // 基本类
        SysArticle sysArticle = sysArticleForm.getSysArticle();
        if (sysArticle == null)
            sysArticle = new SysArticle();

        // 模糊统计
        int totalCount = sysArticleService.countLike(sysArticle);

        // 创建分页
        Page page = sysArticleForm.getPage();
        if (page == null)
            page = new Page();
        page.setTotalCount(totalCount);
        page = sysArticleService.pageManage(page);

        // 模糊分页查询
        model.addAttribute("sysArticles", sysArticleService.listPageLike(page, sysArticle));
        model.addAttribute("page", page);
        model.addAttribute("sysArticle", sysArticle);
        return "sysArticle/listPageSysArticle";
    }

    /**
     * 应用-文章表分页查询列表-执行-异步
     * 
     * @param model
     * @param sysArticleForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ajaxListPage/sysArticle/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson ajaxListPageSysArticleDo(Model model, SysArticleForm sysArticleForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysArticleForm:{}", sysArticleForm);
        }

        // 基本类
        SysArticle sysArticle = sysArticleForm.getSysArticle();
        if (sysArticle == null)
            sysArticle = new SysArticle();

        // 模糊统计
        int totalCount = sysArticleService.countLike(sysArticle);

        // 创建分页
        Page page = sysArticleForm.getPage();
        if (page == null)
            page = new Page();
        page.setTotalCount(totalCount);
        page = sysArticleService.pageManage(page);

        // 模糊分页查询
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> sysArticles = sysArticleService.listPageLike(page, sysArticle);
        // 浏览量赋值
        /*
         * sysArticles.forEach(m -> { BigDecimal count = (BigDecimal) CacheUtils.get("count_" + m.get("article_code"), CacheUtils.COUNT_ARTICLE); if (count != null) m.put("article_click", count.longValue()); });
         */

        map.put("sysArticles", sysArticles);
        map.put("page", page);
        map.put("sysArticle", sysArticle);
        return new SpringJson(true, map);
    }

    /**
     * 应用-文章表分页查询列表-执行-异步
     * 
     * @param model
     * @param sysArticleForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ajaxList/sysArticle/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson ajaxListSysArticleDo(Model model, SysArticleForm sysArticleForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysArticleForm:{}", sysArticleForm);
        }

        // 基本类
        SysArticle sysArticle = sysArticleForm.getSysArticle();
        if (sysArticle == null)
            sysArticle = new SysArticle();

        return new SpringJson(true, sysArticleService.listLike(sysArticle));
    }

    /**
     * 应用-文章表修改跳转-执行
     * 
     * @param model
     * @param sysArticleForm
     * @return
     */
    @RequestMapping("update/sysArticle/to")
    public String updateSysArticleTo(Model model, @Validated({ Update.class }) SysArticleForm sysArticleForm, BindingResult result)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysArticleForm:{}", sysArticleForm);
        }

        // 参数验证-不合法直接返回错误的页面或则错误的json
        if (result.hasErrors())
        {
            model.addAttribute("errors", result.getFieldErrors());
            return "common/error";
        }

        model.addAttribute("sysArticle", sysArticleService.findById(sysArticleForm.getId()));
        return "sysArticle/addOrUpdateSysArticle";
    }

    /**
     * 应用-文章表修改-执行
     * 
     * @param model
     * @param sysArticleForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "update/sysArticle/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson updateSysArticleDo(Model model, SysArticleForm sysArticleForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysArticleForm:{}", sysArticleForm);
        }

        boolean result = sysArticleService.update(sysArticleForm.getSysArticle());
        return new SpringJson(result);
    }

    /**
     * 应用-文章表添加-执行
     * 
     * @param model
     * @param sysArticleForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "add/sysArticle/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson addSysArticleDo(Model model, SysArticleForm sysArticleForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysArticleForm:{}", sysArticleForm);
        }

        SysArticle sysArticle = sysArticleService.add(sysArticleForm.getSysArticle());
        return new SpringJson(sysArticle == null ? false : true);
    }
}